package main

import (
	"encoding/json"
	"fmt"
)

func main() {
	mapSerialization()
	structSerialization()
	sliceSerialization()
}

type Student struct {
	Name  string  `json:"name"`
	Age   int     `json:"age"`
	Score float64 `json:"score"`
}

func mapSerialization() {
	var studentMap map[string]interface{}
	studentMap = make(map[string]interface{}, 10)
	studentMap["name"] = "tom"
	studentMap["age"] = 18

	data, err := json.Marshal(studentMap)
	if err != nil {
		fmt.Println("序列化错误， err：", err)
		return
	}
	fmt.Println("studentMap 序列化后：", string(data))
}

func structSerialization() {
	var studentTom Student = Student{
		Name:  "Tom",
		Age:   18,
		Score: 65.6,
	}
	data, err := json.Marshal(studentTom)
	if err != nil {
		fmt.Println("序列化错误， err：", err)
		return
	}
	fmt.Println("studentTom 序列化后：", string(data))
}

func sliceSerialization() {
	var studentTom Student = Student{
		Name:  "Tom",
		Age:   18,
		Score: 65.6,
	}
	var studentJay Student = Student{
		Name:  "Jay",
		Age:   19,
		Score: 75.6,
	}
	var studentSlice []Student
	studentSlice = append(studentSlice, studentTom)
	studentSlice = append(studentSlice, studentJay)

	data, err := json.Marshal(studentSlice)
	if err != nil {
		fmt.Println("序列化错误， err：", err)
		return
	}
	fmt.Println("studentSlice 序列化后：", string(data))
}
